package cz.mff.kusad5am.hb.ws;

import java.util.List;


import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;
import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;

import cz.mff.kusad5am.hb.schema.list.Hostel;
import cz.mff.kusad5am.hb.schema.reservationlist.HostelReservation;
import cz.mff.kusad5am.hb.schema.roomlist.RoomType;
import cz.mff.kusad5am.hb.service.ListOfRoomsWorker;
import cz.mff.kusad5am.hb.service.ReservationWorker;

public class ReservationEndpoint extends AbstractJDomPayloadEndpoint {

	private final ReservationWorker worker;
	
	public ReservationEndpoint(ReservationWorker worker) {
		this.worker = worker;
	}
	
	
	@Override
	protected Element invokeInternal(Element requestElement) throws Exception {
		
		Namespace ns = requestElement.getNamespace();
		String nsp = ns.getPrefix();
		String start = XPath.newInstance("//"+nsp+":ReservationRequest/@arrivalDate").valueOf(requestElement);
		String guests = XPath.newInstance("//"+nsp+":ReservationRequest/@numberOfGuests").valueOf(requestElement);
		String nights = XPath.newInstance("//"+nsp+":ReservationRequest/@numerOfNights").valueOf(requestElement);
		String hostelId = XPath.newInstance("//"+nsp+":ReservationRequest/"+nsp+":ChosenHostel/@id").valueOf(requestElement);
		String roomId = XPath.newInstance("//"+nsp+":ReservationRequest/"+nsp+":ChosenHostel/"+nsp+":ChosenRoom/@id").valueOf(requestElement);
		String customerId = XPath.newInstance("//"+nsp+":ReservationRequest/"+nsp+":Customer/@id").valueOf(requestElement);
		
		HostelReservation res = worker.createReservation(start, guests, nights, Integer.parseInt(hostelId), Integer.parseInt(roomId), Integer.parseInt(customerId));
		Element response = createResponse(res, ns);
		return response;
	}
	
	private Element createResponse(HostelReservation res, Namespace tns ) {
		Element response = new Element("ReservationResponse");
		response.setNamespace(tns);
//		response.setAttribute("Country", res.getHostel().getCountry());
//		response.setAttribute("City", city);
//		response.setAttribute("arrivalDate", start);
//		response.setAttribute("numberOfGuests", guests);
//		response.setAttribute("numberOfNights", nights);
//		
//		Element hostelWithRooms = new Element("HostelWithRooms");
//		hostelWithRooms.setNamespace(tns);
//		hostelWithRooms.setAttribute("name", hostel.getName());
//		hostelWithRooms.setAttribute("id", new Integer(hostel.getId()).toString());
//		hostelWithRooms.setAttribute("priceDormFrom", hostel.getPriceDormFrom());
//		hostelWithRooms.setAttribute("pricePrivateFrom", hostel.getPricePrivateFrom());
//		hostelWithRooms.setAttribute("Country", hostel.getCountry());
//		hostelWithRooms.setAttribute("City", hostel.getCity());
//		hostelWithRooms.setAttribute("Address", hostel.getAddress());
//		hostelWithRooms.setAttribute("ZipCode", hostel.getZipCode());
//		response.setContent(hostelWithRooms);
//		
//		for(RoomType r: types) {
//			Element child = new Element("RoomType");
//			child.setNamespace(tns);
//			child.setAttribute("isPrivate", r.getIsPrivate());
//			child.setAttribute("bedCount", r.getBedCount());
//			child.setAttribute("hasPrivateShower", r.getHasPrivateShower());
//			child.setAttribute("note", r.getNote());
//			child.setAttribute("priceForNightForPerson", r.getPriceForNightForPerson());
//			child.setAttribute("id", r.getId());
//			hostelWithRooms.addContent(child);
//		}
		return response;
	}
}
